package com.layui.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.layui.entity.MBlog;
import com.layui.entity.vo.QueryBlog;
import com.layui.service.MBlogService;
import com.layui.utils.Message;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-30
 */
@Controller
public class MBlogController {

    @Resource
    private MBlogService blogService;

    @GetMapping({"/","index"})
    public String  index(){
        return "index";
    }

    @GetMapping("findAll")
    @ResponseBody
    public Message FindAll(@RequestParam("page")Integer currentPage, @RequestParam("limit")Integer limit, @RequestParam(name = "QueryBlog",required = false) String  queryBlog){

        QueryBlog query =JSON.parseObject(queryBlog,QueryBlog.class);
        System.out.println(query);
        Page<MBlog> page=new Page<>(currentPage,limit);
        QueryWrapper<MBlog> queryWrapper =new QueryWrapper<>();
        if(query!=null) {
            if (StringUtils.isNotEmpty(query.getTitle())) {
                queryWrapper.like("title", query.getTitle());
            }
            if (StringUtils.isNotEmpty(query.getBegintime())) {
                queryWrapper.ge("begintime", query.getBegintime());
            }
            if (StringUtils.isNotEmpty(query.getUpdatetime())) {
                queryWrapper.le("updatetime", query.getUpdatetime());
            }
        }
        IPage<MBlog> pageInfo = blogService.page(page, queryWrapper);
        Message<MBlog>  message=new Message<MBlog>(0,"传递成功",pageInfo.getTotal(),pageInfo.getRecords());
        return message;
    }
}

